package com.hzqy.service.epg;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hzqy.web.vo.epg.gcw.AcTicketNumVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.service.AuthenticatedService;
import com.hzqy.commons.utils.LastDayUtils;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.epg.AcIntegralDao;
import com.hzqy.service.hqjc.ActivityServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.AcWinningVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;
import com.hzqy.web.vo.epg.AcIntegralVo;
import com.hzqy.web.vo.epg.CmSvcVoteTotalVo;
import com.hzqy.web.vo.epg.gcw.AcStbUserVotenum;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Service
public class AcIntegralService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private AcIntegralDao acIntegralDao;
	private static final int[] activity_Periods = {1,2,3};
	@Resource
	private epgCommonService epgCommonService;
	@Resource
	private ActivityServiceBean activityServiceBean;
	@Resource
	private AcIptvListServiceBean iptvServiceBean;

	private static String Periods() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int now = Integer.parseInt(sdf.format(new Date()));
		String type = "1";
		if(now>=20190328&&now<20190420) {
			type = "1";
		}else if(now>=20190420&&now<20190516) {
			type = "2";
		}else if(now>=20190516) {
			type = "3";
		}
		return type;
	}
	// 社区积分排名
	public Map<String,Object> selectCommunityIntegral(AcIntegralVo aiv,int pageNo,int pageSize) {
		Map<String,Object> map=new HashMap<String, Object>();
		Map<String,Object> map2=new HashMap<String, Object>();
		map.put("aiv_activity",aiv.getAiv_activity());
		map.put("firstResult", pageNo);
		map.put("maxResults", pageSize);
		map.put("aiv_sasId", aiv.getAiv_sasId());//传入社区id查询当前身躯排名
		List<AcIntegralVo> list=acIntegralDao.selectCommunityIntegral(map);
		map2.put("result", list);
		return map2;
	}
	//社区总条数
	public Map<String,Object> selectCommunityCount(AcIntegralVo aiv) {
		Map<String,Object> map=new HashMap<String, Object>();
		Map<String,Object> map2=new HashMap<String, Object>();
		map.put("aiv_activity",aiv.getAiv_activity());
		int count = acIntegralDao.selectCommunityCount(map);
		map2.put("count", count);
		return map2;
	}
	//社区点赞数
	public AcIntegralVo  selectCommunityLike(String aiv_sasId, String aiv_activity,String aiv_sasName) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aiv_activity",aiv_activity);
		map.put("aiv_sasId",aiv_sasId);
		map.put("aiv_sasName",aiv_sasName);
		AcIntegralVo aiv_likeNum = acIntegralDao.selectCommunityLike(map);
		
		return aiv_likeNum;
	}
	
	//社区积分前五
	public  List<AcIntegralVo>  selectAivPhone(AcIntegralVo aiv){
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aiv_activity",aiv.getAiv_activity());
		map.put("aiv_sasId", aiv.getAiv_sasId());
		map.put("aiv_type",Periods());
		List<AcIntegralVo> list=acIntegralDao.selectAivPhone(map);
		return list;
	}
//个人积分排名
	public Map<String,Object> selectIndividualRanking(AcIntegralVo aiv) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aiv_activity",aiv.getAiv_activity());
		map.put("aiv_type",Periods());
		List<AcIntegralVo> list=acIntegralDao.selectIndividualRanking(map);
		Map<String,Object> map2=new HashMap<String, Object>();
		map2.put("allrank", list);
		map.put("aiv_serviceId", aiv.getAiv_serviceId());//传入itv账号，查询当前排名
		List<AcIntegralVo> list2=acIntegralDao.selectIndividualRanking(map);
		map2.put("myrank", list2);
		return map2;
	}
	
	//查询社区id
	public String selectSasId(String f_wtut_serverid){
		String sasid =	acIntegralDao.selectSasId(f_wtut_serverid);
		if(sasid == null) {
			sasid= "0";
		}
		return sasid;
	}
	/**
	 * 查询票数
	 * @param avu
	 * @return
	 */
	public Map<String,Object> selectAcLikeVotenum (ActivityVoteUserVo avu ) {
		Map<String,Object> map = new HashMap<String,Object>();
		int toatl=0;
		int used =0;
		List<ActivityVoteUserVo> avulist = acIntegralDao.selectAcLikeVotenum(avu);
		for(int i=0;i<avulist.size();i++ ) {
			if (avulist.get(i).getF_avu_type() == 2) {
				used = avulist.get(i).getVoteSUM();
			}
			if (avulist.get(i).getF_avu_type() == 3) {
				toatl = avulist.get(i).getVoteSUM();
			}
		}
		map.put("toatl", toatl);
		map.put("used", used);
		if(used > toatl) {
			map.put("surplus", 0);
		}else {
			map.put("surplus", toatl-used);
		}
		return map;
	}
	public List<ActivityVoteUserVo> selectAcLikeVoteList (ActivityVoteUserVo avu ) {
		List<ActivityVoteUserVo> avulist = acIntegralDao.selectAcLikeVoteList(avu);
		return avulist;
	}
	
	/**
	 * 插入投票记录
	 * @param acvote
	 * @return
	 */
	public int insertACLikeVote(ActivityVoteUserVo acvote) {
		return acIntegralDao.insertACLikeVote(acvote);
	}
	@Transactional(rollbackFor = {Exception.class})
	public Map<String,Object> insertACLikeVoteV2(ActivityVoteUserVo acvote) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			int f_avu_votenum = acvote.getF_avu_votenum();
			if(acvote.getF_avu_votenum_reduce() !=0){//广场舞复赛导师助力1.2倍票数，选手加票和用户减票  数量不一致而增加的减票参数
				acvote.setF_avu_votenum(acvote.getF_avu_votenum_reduce());
			}
			LOG.debug(acvote.getF_avu_serverid()+"投票给"+f_avu_votenum+"："+f_avu_votenum+"票--消耗票数："+acvote.getF_avu_contentid());
			int ins = acIntegralDao.insertACLikeVote(acvote);
			CmSvcVoteTotalVo csvt=new CmSvcVoteTotalVo();
			csvt.setF_csvt_activityid(acvote.getF_avu_activityid());
			csvt.setF_csvt_contentid(acvote.getF_avu_contentid()+"");
			List<CmSvcVoteTotalVo> results = acIntegralDao.selectCmSvcVoteTotal(csvt);
			if(results.size()>0){//更新
				csvt.setF_csvt_votecount(f_avu_votenum);
				csvt.setF_csvt_activityid(acvote.getF_avu_activityid());
				acIntegralDao.updateCmSvcVoteTotalVo(csvt);
			}else{//插入
				csvt.setF_csvt_votecount(f_avu_votenum);
				csvt.setF_csvt_status("1");
				acIntegralDao.insertCmSvcVoteTotalVo(csvt);
			}
			map.put("resulet",ins);
			map.put("Mesg","投票成功");
		} catch (Exception e) {
			LOG.error("投票失败:"+e);;
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			map.put("resulet",0);
			map.put("Mesg","投票失败回滚");
		}
		return map;
	}
	//根据电话号码查询社区名称
	public String selectUserSasNameBy(String f_wtut_phone) {
		return acIntegralDao.selectUserSasNameBy(f_wtut_phone);
	}
	public int selectAcContentVotenumCount(int f_catalog_id,String f_cp_cprid,CmContentVo contentVo){
		int results = acIntegralDao.selectAcContentVotenumCount(f_catalog_id,f_cp_cprid,contentVo);
		return results;
	}
	public List<CmContentVo> selectAcContentVotenumList(int f_catalog_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = acIntegralDao.selectAcContentVotenumList(f_catalog_id,pageNo,pageSize,f_cp_cprid,contentVo);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long nowTime=date.getTime();
		try{
			for(int i=0;i<results.size();i++){
				CmContentVo ccVo=results.get(i);
				if(ccVo!=null){
					String startString = ccVo.getF_catalog_starttime();
					String endString = ccVo.getF_catalog_endtime();
					if("".equals(startString) || "".equals(endString) || startString==null ||endString==null){
						ccVo.setF_catalog_isOpen(0);
						ccVo.setSurplus_time("0");
					}else{
						long endTime=sdf.parse(endString).getTime();
						if(date.getTime()>=sdf.parse(startString).getTime() && date.getTime() <=sdf.parse(endString).getTime()){
							ccVo.setF_catalog_isOpen(1);
							long surplus=endTime-nowTime;
							long day=0;
							day = surplus/(1000*3600*24);
							if(surplus%(1000*3600*24)>0){
								day+=1;
							}
							ccVo.setSurplus_time(day+"天");
						}else{
							ccVo.setF_catalog_isOpen(0);
							ccVo.setSurplus_time("0");
						}
					}
				}
			}
		}catch(Exception e){
			LOG.error("内容列表失败",e);
		}
		return results;
	}
	/**
	 * 插入投票总数表
	 * @param csvt
	 * @return
	 */
	public int insertCmSvcVoteTotalVo(CmSvcVoteTotalVo csvt) {
		return acIntegralDao.insertCmSvcVoteTotalVo(csvt);
	}
	/**
	 * 更新投票总数表
	 * @param csvt
	 * @return
	 */
	public int updateCmSvcVoteTotalVo(CmSvcVoteTotalVo csvt) {
		return acIntegralDao.updateCmSvcVoteTotalVo(csvt);
	}
	/**
	 *查询投票总数表
	 * @param csvt
	 * @return
	 */
	public List<CmSvcVoteTotalVo> selectCmSvcVoteTotal(CmSvcVoteTotalVo csvt) {
		List<CmSvcVoteTotalVo> results = acIntegralDao.selectCmSvcVoteTotal(csvt);
		return results;
	}
	/**
	 * 清理缓存数据信息
	 * */
	public List<ActivityVoteUserVo> clearVoteCache(){
		return acIntegralDao.selectClearCacheVote();
	}
	public int insertAcStbUserVotenum(AcStbUserVotenum acvote) {
		return acIntegralDao.insertAcStbUserVotenum(acvote);
	}
	/**
	 * 查询广场舞活动用户可投票数
	 * @param f_asuv_activityid 活动id
	 * @param f_asuv_source  投票来源 1有料签到，2产品开通，3赠票卡',
	 * @param f_asuv_serverid 目标id
	 * @param f_asuv_resolution 高标清  1标清  2高清     0未知   已从session中获取
	 * @param f_asuv_startTime 票数有效时间   当天有效  此接口内部已完成 开始与结束时间提取，不用再传此参数
	 * @param f_asuv_endTime   票数有效结束时间
	 * @return all_count：用户可投票数  day_count:每日基础票数  order_count:订购获得票数  ylqd_count:有料签到获得票数  cpkt_count:产品开通获得票数  zpk_count:赠票卡获得票数
	 */
	public Map<String,Object> selectGCWEffectiveVoteCount(AcStbUserVotenum acvote,HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		String userid= (String) session.getAttribute(SessionKey.SESSION_SERVERID);
		String gd_service_ac_gcw_dayvotes = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_ac_gcw_dayvotes.getKey());
		String gd_service_ac_gcw_dayOrderMaxVotes = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_ac_gcw_dayOrderMaxVotes.getKey());
		Map<String,Object> returnMap =new HashMap<>();
		int dayOrderMaxVotes=0;
		int day_count=0;
		int order_count=0;
		int ylqd_count=0;
		int cpkt_count=0;
		int zpk_count=0;
		int all_count=0;
		String f_asuv_startTime="";
		String f_asuv_endTime="";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		f_asuv_startTime = sdf.format(date).substring(0,11)+"00:00:00";
		f_asuv_endTime = sdf.format(date).substring(0,11)+"23:59:59";
		//用户通过订购获得的最高票数120票
		if(!"".equals(gd_service_ac_gcw_dayOrderMaxVotes)&&!"null".equals(gd_service_ac_gcw_dayOrderMaxVotes)&& null!=gd_service_ac_gcw_dayOrderMaxVotes){
			dayOrderMaxVotes=Integer.parseInt(gd_service_ac_gcw_dayOrderMaxVotes);
		}
		//每日用户基础票数
		if(!"".equals(gd_service_ac_gcw_dayvotes)&&!"null".equals(gd_service_ac_gcw_dayvotes)&& null!=gd_service_ac_gcw_dayvotes){
			day_count=Integer.parseInt(gd_service_ac_gcw_dayvotes);
		}
		all_count += day_count;
		
		//鉴权
		Map<String,Object> map = new HashMap<>();
		try {
			map = epgCommonService.getOrderType(userid,request,response);
		} catch (Exception e) {
			LOG.error("用户已订购产品详情查询失败:",e);
		}
		
		if("0".equals(map.get("filmAll"))){//filmAll 合家欢 包括 影视 和 超级少儿
			order_count+=80;
		}else{
			if("0".equals(map.get("mtv_ThirdLine")) || "0".equals(map.get("mtv_month"))){//影视剧  包括电影 电视剧  
				order_count+=50;
			}else{
				if("0".equals(map.get("mv_ThirdLine")) || "0".equals(map.get("mv_month"))){//电影
					order_count+=20;
				}
				if("0".equals(map.get("tv_ThirdLine")) || "0".equals(map.get("tv_month"))){//电视剧
					order_count+=30;
				}
			}
			/*
			 * if("0".equals(map.get("kid_ThirdLine")) ||
			 * "0".equals(map.get("kid_month"))){//超级少儿 order_count+=30; }
			 */
		}
		if("0".equals(map.get("xm_month"))){//戏迷天地
			//order_count+=20;
			order_count+=40;
		}
		if("0".equals(map.get("music_month"))  || "0".equals(map.get("music_ThirdLine"))){//音乐
			order_count+=20;
		}
		String kidresult = "504";
		try {
			kidresult = serviceAuth( userid, "00000001000000110000000000000406", request, response);
		}catch(Exception e) {
			//LOG.error("超级少儿鉴权异常 用户："+userid+","+e.getMessage());
		}
		if(kidresult.equals("0") ){//超级少儿
			order_count+=30;
		}
		
		order_count=order_count>dayOrderMaxVotes?dayOrderMaxVotes:order_count;//订购最多可得120
		all_count += order_count;
				
		//查询已领取票数
		acvote.setF_asuv_serverid(userid);
		acvote.setF_asuv_startTime(f_asuv_startTime);
		acvote.setF_asuv_endTime(f_asuv_endTime);
		List<AcStbUserVotenum> VoteList = acIntegralDao.selectAcStbUserVotenumList(acvote);
		if(VoteList!=null && VoteList.size()>0){
			for(int i=0;i<VoteList.size();i++){
				if("1".equals(VoteList.get(i).getF_asuv_source())){
					ylqd_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("2".equals(VoteList.get(i).getF_asuv_source())){
					cpkt_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("3".equals(VoteList.get(i).getF_asuv_source())){
					zpk_count +=VoteList.get(i).getF_asuv_votenum();
				}
				all_count+=VoteList.get(i).getF_asuv_votenum();
			}
		}
		
		//查询已投票数
		ActivityVoteUserVo acv=new ActivityVoteUserVo();
		acv.setF_avu_serverid(userid);
		acv.setF_avu_activityid(acvote.getF_asuv_activityid());
		acv.setF_avu_startTime(f_asuv_startTime);
		acv.setF_avu_endTime(f_asuv_endTime);
		Map<String,Object> usedmap = new HashMap<String,Object>();
		int used_count=0;
		try{
			usedmap = selectAcLikeVotenum(acv);
			used_count = Integer.parseInt(usedmap.get("used").toString());
		}catch(Exception e){
			all_count=0;//已投票数查询失败，可投票数置0，避免无限投票
			LOG.error("查询已投票数失败:",e);
		}
		all_count -= used_count;
		all_count = all_count<0?0:all_count;
		
		returnMap.put("all_count",all_count);
		returnMap.put("day_count",day_count);
		returnMap.put("order_count",order_count);
		returnMap.put("ylqd_count",ylqd_count);
		returnMap.put("cpkt_count",cpkt_count);
		returnMap.put("zpk_count",zpk_count);
		
		return returnMap;
	}
	/**
	 * @Description: 查询是否增票
	 * @Param: * @param 0.兑换成功 1，兑换失败 2.账号错误
	 * @return:
	 * @Author: LiuL
	 * @Date: 2019/5/15
	 */
	@Transactional(rollbackFor = {Exception.class})
	public Map<String,String> addVoteNum(String f_at_password,String userid,String resolution,String activityid) {
		AcTicketNumVo acTicketNumVo = acIntegralDao.selectAcTicketNum(f_at_password);
		Map<String,String> map=new HashMap<String,String>();
		Map<String,String> result=new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String today = sdf.format(d);
		int countNum=0;
		AcStbUserVotenum stbnewNum=new AcStbUserVotenum();
		stbnewNum.setF_asuv_serverid(userid);
		stbnewNum.setF_asuv_source("3");
		stbnewNum.setF_asuv_activityid(activityid);
		stbnewNum.setF_asuv_memo("广场舞增票卡");
		stbnewNum.setF_asuv_obligate1(1);
		stbnewNum.setF_asuv_obligate2("1");
		stbnewNum.setF_asuv_resolution(Integer.parseInt(resolution));
		map.put("f_at_password",f_at_password);
		map.put("f_at_serverid",userid);
		map.put("f_at_resolution",resolution);
		map.put("f_at_udate",today);
		countNum = acIntegralDao.selectCountNum(map);//查询已经兑换的次数
		if (acTicketNumVo != null &&countNum<30){
			result.put("note",acTicketNumVo.getF_at_voteNum());
			stbnewNum.setF_asuv_resolution(Integer.parseInt(resolution));
			stbnewNum.setF_asuv_votenum(Integer.parseInt(acTicketNumVo.getF_at_voteNum()));
				try {
					acIntegralDao.updateAcTicketNum(map);//更改成已经领取状态
					//acIntegralDao.insertAcStbUserVotenum(stbnewNum);
					insertUserVotenum(stbnewNum);
					result.put("result","0");
					result.put("Mesg","兑换成功");
				} catch (Exception e) {
					LOG.error("兑换失败:",e);;
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					result.put("result","1");//兑换回滚请稍后再试
					result.put("Mesg","兑换失败回滚");
				}
		}else {//
			result.put("result","2");//卡密错误
			result.put("Mesg","卡密错误");
		}
		return result;
	}
	
	/**
	 * 服务鉴权
	 */
	private String  serviceAuth(String userID,String serviceID,HttpServletRequest request,HttpServletResponse response) {
		int readTimeOut = 3000;
		int connectTimeout = 3000;
		AuthenticatedService authenticatedService = new AuthenticatedService(request,response);
		return authenticatedService.getAuthenticatedResult(userID,serviceID,readTimeOut,connectTimeout);
	}
	/**
	 * @Description: 查询品质标验证码
	 * @Param: * @param 0.兑换成功 1，兑换失败 2.账号错误
	 * @return:
	 * @Author: LiuL
	 * @Date: 2019/9/11
	 */
	public Map<String,String> selectPzbCodeNum(String userid,String resolution,String activityid) {
		Map<String,String> map=new HashMap<String,String>();
		Map<String,String> result=new HashMap<>();
		map.put("f_at_openid",activityid);// 活动id
		//先查询是否存在已经获取的验证码
		map.put("f_at_serverid",userid);
		String num = acIntegralDao.selectPzbCode(map);
		//没有去查询
        if(num==null||num==""||num.length()==0){
			AcTicketNumVo acTicketNumVo = acIntegralDao.selectPzbCodeNum(map);
			LOG.debug("******品质标验证码****"+acTicketNumVo.getF_at_password());
			if(acTicketNumVo!=null){
				map.put("f_at_password",acTicketNumVo.getF_at_password());
				map.put("f_at_resolution",resolution);
				int i = acIntegralDao.updatePzbCodeNum(map);
				result.put("success",String.valueOf(i));//1 是成功 0是失败
				result.put("result",acTicketNumVo.getF_at_password());// 验证码
				return result;
			}else{
				result.put("success","0");
				result.put("result","验证码不足");
				return result;
			}
		}else {
			result.put("success",String.valueOf(1));//1 是成功 0是失败
			result.put("result",num);// 验证码
			return result;
		}
	}
	/**
	 * 用户屯票，插入流水表，记录总表
	 * @param acvote 
	 * f_asuv_serverid,用户id;f_asuv_activityid:活动编号；
	 * f_asuv_votenum：票数；f_asuv_source：票数来源；f_asuv_memo：备注，建议携带
	 * 总表中使用f_csvt_contentid来存放用户id
	 * @return 
	 */
	@Transactional(rollbackFor = {Exception.class})
	public Map<String,Object> insertUserVotenum(AcStbUserVotenum acvote) {
		Map<String,Object> map = new HashMap<String,Object>();
		int gd_service_ac_gcw_openTime = Integer.valueOf(SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_ac_gcw_openTime.getKey()));
		int gd_service_ac_gcw_closeTime = Integer.valueOf(SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_ac_gcw_closeTime.getKey()));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		boolean flag = false;
		try {
			int time = Integer.valueOf(sdf.format(new Date()));
			if(time>= gd_service_ac_gcw_openTime && time <= gd_service_ac_gcw_closeTime) {
				flag = true;
			}
		} catch (Exception e) {
			LOG.error("时间错误:"+acvote.getF_asuv_serverid()+";"+e);
		}
		if(!flag) {
			map.put("resulet",1);
			map.put("Mesg","时间未到");
			LOG.error("不在囤票时间内，不囤票入库:"+acvote.getF_asuv_serverid()+"--"+acvote.getF_asuv_memo());
			return map;
		}
		try {
			acIntegralDao.insertAcStbUserVotenum(acvote);
			CmSvcVoteTotalVo csvt=new CmSvcVoteTotalVo();
			csvt.setF_csvt_activityid(acvote.getF_asuv_activityid());
			csvt.setF_csvt_contentid("0");
			csvt.setF_csvt_obligate1(acvote.getF_asuv_serverid());
			List<CmSvcVoteTotalVo> results = acIntegralDao.selectCmSvcVoteTotal(csvt);
			if(results.size()>0){//更新
				LOG.debug("票数修改"+csvt.getF_csvt_obligate1());
				csvt.setF_csvt_votecount(acvote.getF_asuv_votenum());
				acIntegralDao.updateCmSvcVoteTotalVo(csvt);
			}else{//插入
				LOG.debug("票数新增"+csvt.getF_csvt_obligate1());
				csvt.setF_csvt_votecount(acvote.getF_asuv_votenum());
				csvt.setF_csvt_status("1");
				acIntegralDao.insertCmSvcVoteTotalVo(csvt);
			}
			map.put("resulet",0);
			map.put("Mesg","屯票成功");
			LOG.debug("屯票成功:"+acvote.getF_asuv_serverid()+"，票数="+acvote.getF_asuv_votenum());
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			LOG.error("屯票失败:"+acvote.getF_asuv_serverid()+";"+e);
			map.put("resulet",-1);
			map.put("Mesg","屯票失败回滚");
		}
		return map;
	}
	/**
	 * 用户每日票数入库 source：4表示订购屯票，5表示每日送票
	 * @param acvote
	 * f_asuv_serverid,用户id;f_asuv_activityid:活动编号
	 * @return
	 */
	public Map<String,Object> insertUserVotenumByDay(AcStbUserVotenum acvote,HttpServletRequest request,HttpServletResponse response ) {
		Map<String,Object> map = new HashMap<String,Object>();
		//先查询有没有存放过数据有
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(new Date());
		acvote.setF_asuv_startTime(time+" 00:00:00");
		acvote.setF_asuv_endTime(time+" 23:59:59");
		acvote.setF_asuv_source("4,5");//4表示订购屯票，5表示每日送票
		List<AcStbUserVotenum> list = acIntegralDao.selectAcStbUserVotenumList(acvote);
		boolean orderFlag = false;
		boolean dayFlag = true;
		int orderVote = 0;
		LOG.debug(acvote.getF_asuv_serverid()+"用户当天屯票列："+list.size());
		if(list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				if("4".equals(list.get(i).getF_asuv_source())) {
					orderVote += list.get(i).getF_asuv_votenum();
				}
				if("5".equals(list.get(i).getF_asuv_source())) {
					dayFlag = false;
				}
			}
		}
		if(dayFlag) {
			//广场舞每日票数
			String gd_service_ac_gcw_dayvotes = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_ac_gcw_dayvotes.getKey());
			int day_count = 0;
			if(!"".equals(gd_service_ac_gcw_dayvotes)&&!"null".equals(gd_service_ac_gcw_dayvotes)&& null!=gd_service_ac_gcw_dayvotes){
				day_count=Integer.parseInt(gd_service_ac_gcw_dayvotes);
			}
			acvote.setF_asuv_source("5");
			acvote.setF_asuv_votenum(day_count);
			acvote.setF_asuv_memo("每日签到屯票");
			insertUserVotenum(acvote);
			LOG.debug("签到屯票");
		}
		int orderVoteAll = getVoteByOrder(acvote.getF_asuv_serverid(),request,response);
		LOG.debug(acvote.getF_asuv_serverid()+"订购的票："+orderVoteAll+";已屯票"+orderVote);
		if(orderVoteAll>orderVote) {
			orderFlag = true;
			acvote.setF_asuv_source("4");
			acvote.setF_asuv_votenum(orderVoteAll - orderVote);
			acvote.setF_asuv_memo("每日订购屯票");
			insertUserVotenum(acvote);
			LOG.debug("每日订购屯票");
		}
		if(!orderFlag && !dayFlag) {
			map.put("result", 1);
			map.put("msg", "今日已屯票");
		}else if(orderFlag && !dayFlag) {
			map.put("result", 2);
			map.put("msg", "今日订购屯票");
		}else if(orderFlag && dayFlag) {
			map.put("result", 0);
			map.put("msg", "今日屯票成功");
		}
		return map;
	}
	
	//计算用户订购得票
	public int getVoteByOrder(String userid,HttpServletRequest request,HttpServletResponse response ) {
		int order_count=0;
		//鉴权
		Map<String,Object> map = new HashMap<>();
		try {
			map = epgCommonService.getOrderType(userid,request,response);
		} catch (Exception e) {
			LOG.error("用户已订购产品详情查询失败:",e);
		}
		
		if("0".equals(map.get("filmAll"))){//filmAll 合家欢 包括 影视 和 超级少儿
			order_count+=80;
		}else{
			if("0".equals(map.get("mtv_ThirdLine")) || "0".equals(map.get("mtv_month"))){//影视剧  包括电影 电视剧  
				order_count+=50;
			}else{
				if("0".equals(map.get("mv_ThirdLine")) || "0".equals(map.get("mv_month"))){//电影
					order_count+=20;
				}
				if("0".equals(map.get("tv_ThirdLine")) || "0".equals(map.get("tv_month"))){//电视剧
					order_count+=30;
				}
			}
			/*
			 * if("0".equals(map.get("kid_ThirdLine")) ||
			 * "0".equals(map.get("kid_month"))){//超级少儿 order_count+=30; }
			 */
		}
		if("0".equals(map.get("xm_ThirdLine")) || "0".equals(map.get("xm_month"))){//戏迷天地
			//order_count+=20;
			order_count+=40;
		}
		if("0".equals(map.get("music_month"))  || "0".equals(map.get("music_ThirdLine"))){//音乐
			order_count+=20;
		}
		String kidresult = "504";
		try {
			kidresult = serviceAuth( userid, "00000001000000110000000000000406", request, response);
		}catch(Exception e) {
			//LOG.error("超级少儿鉴权异常 用户："+userid+","+e.getMessage());
		}
		if("0".equals(kidresult) ){//超级少儿
			order_count+=30;
		}
		//广场舞订购最大票数
		String gd_service_ac_gcw_dayOrderMaxVotes = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_ac_gcw_dayOrderMaxVotes.getKey());
		int dayOrderMaxVotes = 0;
		if(!"".equals(gd_service_ac_gcw_dayOrderMaxVotes)&&!"null".equals(gd_service_ac_gcw_dayOrderMaxVotes)&& null!=gd_service_ac_gcw_dayOrderMaxVotes){
			dayOrderMaxVotes=Integer.parseInt(gd_service_ac_gcw_dayOrderMaxVotes);
		}
		//订购最多可得140
		order_count=order_count>dayOrderMaxVotes?dayOrderMaxVotes:order_count;
		return order_count;
	}
	public Map<String,Object> selectVoteTotalRownum(String userid, String activityid,int start,int pageSize,String type,String voteid) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("f_csvt_activityid", activityid);
		map.put("start", start);
		map.put("pageSize", pageSize);
		if("1".equals(type)||"0".equals(type)) {
			List<CmSvcVoteTotalVo> allList = acIntegralDao.selectVoteTotalRownum(map);
			map.put("allList", allList);
		}
		if("3".equals(type)||"2".equals(type)||"0".equals(type)) {
			map.put("f_csvt_obligate1", userid);
			List<CmSvcVoteTotalVo>  myList= acIntegralDao.selectVoteTotalRownum(map);
			map.put("myList", myList);
		}
		if("3".equals(type)) {
			ActivityVoteUserVo acvote =new ActivityVoteUserVo();
			acvote.setF_avu_contentid(0);
			acvote.setF_avu_serverid(userid);
			acvote.setF_avu_activityid(voteid);
			Map<String, Object> voteMap = selectAcLikeVotenum(acvote);
			map.put("used", voteMap.get("used"));
		}
		return map;
	}
	
	public Map<String,Object> getGCWLastDay() {
		Map<String,Object> map=new HashMap<String,Object>();
		String gd_service_lastDay = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_voteStartDay.getKey());
		String gd_service_voteEndDay = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_service_voteEndDay.getKey());
		map=LastDayUtils.getLastDay(gd_service_lastDay);
		if(map!=null&&(Boolean)map.get("success")){
			if("0天".equals(map.get("result"))){
				map=LastDayUtils.getLastDay(gd_service_voteEndDay);
				map.put("voteStarting",true);
			}else{
				map.put("voteStarting",false);
			}
			if(map==null || !(Boolean)map.get("success")){
				map.put("result", "");
				map.put("success", false);
			}
		}else{
			map.put("result", "");
			map.put("success", false);
		}
		return map;
	}
	public Map<String,Object> selectGCWRepechargeDayVotes(AcStbUserVotenum acvote,HttpServletRequest request){
		Map<String,Object> returnMap=new HashMap<String,Object>();
		HttpSession session = request.getSession();
		String userid= (String) session.getAttribute(SessionKey.SESSION_SERVERID);
		String f_asuv_startTime="";
		String f_asuv_endTime="";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		f_asuv_startTime = sdf.format(date).substring(0,11)+"00:00:00";
		f_asuv_endTime = sdf.format(date).substring(0,11)+"23:59:59";
		int day_count=0;
		int order_count=0;
		int ylqd_count=0;
		int cpkt_count=0;
		int zpk_count=0;
		int lott_count=0;
		int all_count=0;
		//查询已领取票数
		acvote.setF_asuv_serverid(userid);
		acvote.setF_asuv_startTime(f_asuv_startTime);
		acvote.setF_asuv_endTime(f_asuv_endTime);
		List<AcStbUserVotenum> VoteList = acIntegralDao.selectAcStbUserVotenumList(acvote);
		if(VoteList!=null && VoteList.size()>0){
			for(int i=0;i<VoteList.size();i++){
				if("1".equals(VoteList.get(i).getF_asuv_source())){
					ylqd_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("2".equals(VoteList.get(i).getF_asuv_source())){
					cpkt_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("3".equals(VoteList.get(i).getF_asuv_source())){
					zpk_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("4".equals(VoteList.get(i).getF_asuv_source())){
					order_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("5".equals(VoteList.get(i).getF_asuv_source())){
					day_count +=VoteList.get(i).getF_asuv_votenum();
				}else if("6".equals(VoteList.get(i).getF_asuv_source())){
					lott_count +=VoteList.get(i).getF_asuv_votenum();
				}
			}
		}
		returnMap.put("day_count",day_count);
		returnMap.put("order_count",order_count);
		returnMap.put("ylqd_count",ylqd_count);
		returnMap.put("cpkt_count",cpkt_count);
		returnMap.put("zpk_count",zpk_count);
		returnMap.put("lott_count",lott_count);
		return returnMap;
	}
	public Map<String,Object> updatePlayTime(AcWinningVo awv,int limitTime,String status){
		Map<String,Object> map=new HashMap<String, Object>();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String time1 = sdf.format(date);
	    String key = awv.getF_awl_activityid()+"_"+awv.getF_awl_serverid();
	    awv.setF_awl_date(time1);
		if(status!=null && "0".equals(status)){
			RedisService.setPlayTime(key,String.valueOf(RedisService.getTime()), 60*60*12);//默认存储12小时
			map.put("updateTime",time1);
			LOG.debug("播放开始/serverid:"+awv.getF_awl_serverid());
		}else if(status!=null && "1".equals(status)){
			String playTime = RedisService.getPlayTimeString(key);
			RedisService.removePlayTimeString(key);//播放结束清除记录
			long pt =0;
			if(playTime != null){
				 pt = Long.parseLong(playTime);
				 pt = RedisService.getTime() - pt;
			}
			pt=pt/1000;
			awv.setF_awl_userphone(pt+"");
			Boolean flag=true;
			if(pt >= limitTime){//观看达到时长 
				 List<AcWinningVo> lists=activityServiceBean.selectUWinningList(awv);
				 if(lists.size()<=0){//不存在
					 flag=false;
				 }
			}
			if(pt >= 10){//观看超过10秒才入库
				try{
					if(!flag){
						awv.setF_awl_type("3");
						activityServiceBean.insertWinningList(awv);
					}
					AcStbUserVotenum acvote = new AcStbUserVotenum();
					acvote.setF_asuv_activityid(awv.getF_awl_activityid());
					acvote.setF_asuv_serverid(awv.getF_awl_serverid());
					acvote.setF_asuv_source(awv.getF_awl_prizeid());
					acvote.setF_asuv_votenum((int)pt);
					acvote.setF_asuv_memo(awv.getF_awl_prizename());
					acIntegralDao.insertAcStbUserVotenum(acvote);
				}catch(Exception e){
					//LOG.error("记录入库异常:"+e.getMessage());
				}
			}
			map.put("playTime",pt);
			LOG.debug("播放开始时间:"+playTime+"/观看总时间:"+pt+"/updateTime:"+time1+"/serverid:"+awv.getF_awl_serverid()+"/activityid:"+awv.getF_awl_activityid()+"/关卡:"+awv.getF_awl_prizeid());
		}
		map.put("status",status);
		return map;
	}
	/**
	 * 动物园直播，喂食，插入流水表，记录总表
	 * @param acvote 
	 * f_asuv_serverid,用户id;f_asuv_activityid:活动编号；
	 * f_asuv_votenum：喂食口数；f_asuv_source：来源；f_asuv_memo：备注，建议携带
	 * 总表中使用f_csvt_contentid来存放用户id
	 * @return 
	 */
	@Transactional(rollbackFor = {Exception.class})
	public Map<String,Object> insertFeedNum(AcStbUserVotenum acvote) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			acIntegralDao.insertAcStbUserVotenum(acvote);
			CmSvcVoteTotalVo csvt=new CmSvcVoteTotalVo();
			csvt.setF_csvt_activityid(acvote.getF_asuv_activityid());
			csvt.setF_csvt_contentid("0");
			csvt.setF_csvt_obligate1(acvote.getF_asuv_serverid());
			List<CmSvcVoteTotalVo> results = acIntegralDao.selectCmSvcVoteTotal(csvt);
			csvt.setF_csvt_votecount(acvote.getF_asuv_votenum());
			if(results.size()>0){//更新
				LOG.debug("票数修改"+csvt.getF_csvt_obligate1());
				acIntegralDao.updateCmSvcVoteTotalVo(csvt);
			}else{//插入
				LOG.debug("票数新增"+csvt.getF_csvt_obligate1());
				csvt.setF_csvt_status("1");
				acIntegralDao.insertCmSvcVoteTotalVo(csvt);
			}
			map.put("result",0);
			map.put("Mesg","成功");
			LOG.debug("成功:"+acvote.getF_asuv_serverid()+"，票数="+acvote.getF_asuv_votenum());
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			LOG.error("失败:"+acvote.getF_asuv_serverid()+";"+e);
			map.put("result",-1);
			map.put("Mesg","失败回滚");
		}
		return map;
	}
	/*
	 * 查询当天投喂次数
	 */
	public List<AcStbUserVotenum> selectAcStbUserVotenumList(AcStbUserVotenum acvote) {
		List<AcStbUserVotenum> list = acIntegralDao.selectAcStbUserVotenumList(acvote);
		return list;
	}
	public Map<String,Object> policePlayTime(AcStbUserVotenum acvote,int limitTime,int limitPlayTimes,String status,String activityCode,String extraCode,String remark,String sync){
		Map<String,Object> map=new HashMap<String, Object>();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String time1 = sdf.format(date);
	    String key = acvote.getF_asuv_activityid()+"_"+acvote.getF_asuv_serverid();
	    String statusCode="200";
		statusCode=(sync!=null&&"1".equals(sync))?"500":statusCode;
		if(status!=null && "0".equals(status)){
			RedisService.setPlayTime(key,String.valueOf(RedisService.getTime()), 60*60*12);//默认存储12小时
			map.put("updateTime",time1);
			LOG.debug("播放开始/serverid:"+acvote.getF_asuv_serverid());
		}else if(status!=null && "1".equals(status)){
			String playTime = RedisService.getPlayTimeString(key);
			RedisService.removePlayTimeString(key);//播放结束清除记录
			long pt =0;
			if(playTime != null){
				 pt = Long.parseLong(playTime);
				 pt = RedisService.getTime() - pt;
			}
			pt=pt/1000;
			List<AcStbUserVotenum> list = selectAcStbUserVotenumList(acvote);
			Boolean success=true;
			for(int i=0;i<list.size();i++){//是否已看过
				if(acvote.getF_asuv_memo().equals(list.get(i).f_asuv_memo)){
					success=false;
				}
			}
			success=success&&list.size()<limitPlayTimes?true:false;//是否超过每天限制数量
			if(pt >= limitTime && success){//观看超过 limitTime秒才入库
				//先同步积分 后入库
				Map<String,Object> syncMap = iptvServiceBean.pointsProduce(acvote.getF_asuv_serverid(),activityCode,extraCode,remark,acvote.getF_asuv_votenum(),acvote.getF_asuv_activityid());
				LOG.debug("交警专区观看视频送积分同步结果，userid："+acvote.getF_asuv_serverid()+",syncMap:"+syncMap.toString());
				if(statusCode.equals(syncMap.get("status").toString())){
					Map<String,Object> fiveMap = insertFeedNum(acvote);//记录积分
				}
			}
			map.put("playTime",pt);
			LOG.debug("播放结束:/serverid:"+acvote.getF_asuv_serverid()+"播放开始时间:"+playTime+"/观看总时间:"+pt+"/updateTime:"+time1+"/activityid:"+acvote.getF_asuv_activityid());
		}
		map.put("status",status);
		return map;
	}
	/*
	 * 查询已填问卷情况
	 */
	public List<AcStbUserVotenum> selectQuestionnaire(AcStbUserVotenum acvote) {
		List<AcStbUserVotenum> list = acIntegralDao.selectAcStbUserVotenumList(acvote);
		return list;
	}
	
	/**
	 * 问卷，插入流水表
	 * @param acvote 
	 * f_asuv_serverid,用户id;f_asuv_activityid:活动编号；
	 * f_asuv_votenum：1；f_asuv_source：来源；f_asuv_memo：备注，问卷答案
	 * @return 
	 */
	@Transactional(rollbackFor = {Exception.class})
	public Map<String,Object> insertQuestionnaire(AcStbUserVotenum acvote) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			int result = acIntegralDao.insertAcStbUserVotenum(acvote);
			map.put("result",result);
			LOG.debug("成功:"+acvote.getF_asuv_serverid()+"，问卷答案="+acvote.getF_asuv_memo());
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			LOG.error("失败:"+acvote.getF_asuv_serverid()+";"+e);
			map.put("result",-1);
			map.put("Mesg","失败回滚");
		}
		return map;
	}
	
	public Map<String,Object> getPlayTime(AcWinningVo awv,String status){
		Map<String,Object> map=new HashMap<String, Object>();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String time1 = sdf.format(date);
	    String key = awv.getF_awl_activityid()+"_"+awv.getF_awl_serverid();
	    awv.setF_awl_date(time1);
		if(status!=null && "0".equals(status)){
			RedisService.setPlayTime(key,String.valueOf(RedisService.getTime()), 60*60*12);//默认存储12小时
			map.put("getTime",time1);
			LOG.debug("播放开始/serverid:"+awv.getF_awl_serverid());
		}else if(status!=null && "1".equals(status)){
			String playTime = RedisService.getPlayTimeString(key);
			RedisService.removePlayTimeString(key);//播放结束清除记录 
			long pt =0; 
			if(playTime !=null){ 
				pt = Long.parseLong(playTime); 
				pt = RedisService.getTime() - pt; 
			}
			pt=pt/1000;
			map.put("playTime",pt);
			LOG.debug("播放开始时间:"+playTime+"/观看总时间:"+pt+"/updateTime:"+time1+"/serverid:"+awv.getF_awl_serverid()+"/activityid:"+awv.getF_awl_activityid());
		}
		map.put("status",status);
		return map;
	}
	
	/*
	 * 查询或者添加用户积分
	 */
	public Map<String,Object> selectOrInsertIntegralCount(AcStbUserVotenum acvote,String status,int selectCount) {

		Map<String,Object> map=new HashMap<String,Object>();

		if(status!=null && "0".equals(status)){
			//查询当天该用户已经获取多少积分
			List<AcStbUserVotenum> list= selectAcStbUserVotenumList( acvote);
			map.put("acStbUserVotenumList", list);
		}else if(status!=null && "1".equals(status)){
			//添加积分
			map = insertFeedNum(acvote);
		}else if(status!=null && "2".equals(status)){
			//查询积分排名
			Map<String,Object> randMap = new HashMap<String,Object>();
			randMap = selectVoteTotalRownum("",acvote.getF_asuv_activityid(),0,selectCount,"2","");
			
			List<CmSvcVoteTotalVo>  myList= (List<CmSvcVoteTotalVo>) randMap.get("myList");
			for (int i = 0; i < myList.size(); i++) {
				if(myList.get(i).getF_csvt_obligate1()==null||"".equals(myList.get(i).getF_csvt_obligate1())) {
					
				}else {
					String serverid = myList.get(i).getF_csvt_obligate1().substring(0,4)+"***"+
							myList.get(i).getF_csvt_obligate1().substring(myList.get(i).getF_csvt_obligate1().length()-4,myList.get(i).getF_csvt_obligate1().length());
					myList.get(i).setF_csvt_obligate1(serverid);
				}
			}
			
			map.put("myList", myList);
		}else if(status!=null && "3".equals(status)){
			//将积分转为奖品
			Map<String,Object> randMap = new HashMap<String,Object>();
			randMap = selectVoteTotalRownum("",acvote.getF_asuv_activityid(),0,selectCount,"2","");
			
			List<CmSvcVoteTotalVo>  myList= (List<CmSvcVoteTotalVo>)randMap.get("myList");
			
			for (int i=0;i<myList.size();i++){
				AcWinningVo awv = new AcWinningVo();
				awv.setF_awl_activityid(acvote.getF_asuv_activityid());
				awv.setF_awl_serverid(myList.get(i).getF_csvt_obligate1());
				Date date=new Date();
			    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    String time1 = sdf.format(date);
			    awv.setF_awl_date(time1);
				if(i==0){
					awv.setF_awl_prizename("1000元礼品卡");
					awv.setF_awl_prizeid("2020080701");
					activityServiceBean.insertWinningList(awv);
				}else if(i>=1&&i<=3){
					awv.setF_awl_prizename("500元礼品卡");
					awv.setF_awl_prizeid("2020080702");
					activityServiceBean.insertWinningList(awv);
				}else if(i>=4&&i<=13){
					awv.setF_awl_prizename("200元礼品卡");
					awv.setF_awl_prizeid("2020080703");
					activityServiceBean.insertWinningList(awv);
				}
			}
			map.put("result",0);
			map.put("Mesg","成功");
		}
		return map;
	}
	
	/*	
	 * 	将用户投票信息转为中奖信息
	*/
	public Map<String,Object> convertVoteToWinningInformation(ActivityVoteUserVo avu,int allCount) {

		Map<String,Object> map=new HashMap<String,Object>();
		
		List<ActivityVoteUserVo> avulist = acIntegralDao.selectAcLikeVotenumByActivityId(avu);
		
		if(avulist.size()>allCount) {
			//投票人数大于送奖品的人数的时候-以送奖品人数为准
			for(int i=0;i<allCount;i++ ) {
				AcWinningVo awv = new AcWinningVo();
				awv.setF_awl_activityid(avulist.get(i).getF_avu_activityid());
				awv.setF_awl_serverid(avulist.get(i).getF_avu_serverid());
				Date date=new Date();
			    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    String time1 = sdf.format(date);
			    awv.setF_awl_date(time1);
			    awv.setF_awl_prizename("星巴克星礼卡");
				awv.setF_awl_prizeid("2020110501");
				awv.setF_awl_nickname(String.valueOf(avulist.get(i).getVoteSUM()));
				activityServiceBean.insertWinningList(awv);
			}
		}else {
			//投票人数小于送奖品的人数的时候-以投票人数为准
			for(int i=0;i<avulist.size();i++ ) {
				AcWinningVo awv = new AcWinningVo();
				awv.setF_awl_activityid(avulist.get(i).getF_avu_activityid());
				awv.setF_awl_serverid(avulist.get(i).getF_avu_serverid());
				Date date=new Date();
			    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    String time1 = sdf.format(date);
			    awv.setF_awl_date(time1);
			    awv.setF_awl_prizename("星巴克星礼卡");
				awv.setF_awl_prizeid("2020110501");
				awv.setF_awl_nickname(String.valueOf(avulist.get(i).getVoteSUM()));
				activityServiceBean.insertWinningList(awv);
			}
		}
		
		map.put("result",0);
		map.put("Mesg","成功");
		
		return map;
	}
	
	public Map<String,Object> getTrackingKingPlayTime(AcWinningVo awv,String status,String convertIntegral,String masterThreshold,String kingThreshold,String pointsLimit){
		Map<String,Object> map=new HashMap<String, Object>();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String time1 = sdf.format(date);
	    String key = awv.getF_awl_activityid()+"_"+awv.getF_awl_serverid();
	    awv.setF_awl_date(time1);
		if(status!=null && "0".equals(status)){
			RedisService.setPlayTime(key,String.valueOf(RedisService.getTime()), 60*60*12);//默认存储12小时
			map.put("getTime",time1);
			LOG.debug("播放开始/serverid:"+awv.getF_awl_serverid());
		}else if(status!=null && "1".equals(status)){
			String playTime = RedisService.getPlayTimeString(key);
			RedisService.removePlayTimeString(key);//播放结束清除记录 
			long pt =0; 
			if(playTime !=null){ 
				pt = Long.parseLong(playTime); 
				pt = RedisService.getTime() - pt;
				pt=pt/1000;//观看单位为秒,观看了多少秒
				//时长除以转换时长获得积分
				long integral = pt/Long.parseLong(convertIntegral);//观看视频得到的积分数
				
				if(integral<=0){
					map.put("playTime",pt);
					LOG.debug("播放开始时间:"+playTime+"/观看总时间:"+pt+"/updateTime:"+time1+"/serverid:"+awv.getF_awl_serverid()+"/activityid:"+awv.getF_awl_activityid());
					map.put("status",status);
					return map;
				}
				//查询今天已经获得的积分
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				String time = sdf1.format(new Date());
				AcStbUserVotenum acvote = new AcStbUserVotenum();
				acvote.setF_asuv_startTime(time+" 00:00:00");
				acvote.setF_asuv_endTime(time+" 23:59:59");
				acvote.setF_asuv_serverid(awv.getF_awl_serverid());
				acvote.setF_asuv_resolution(awv.getF_ui_resolution());
				//查询当天该用户已经获取多少积分
				List<AcStbUserVotenum> acStbUserVotenumList= selectAcStbUserVotenumList( acvote);
				int integralCount = 0;
				if(acStbUserVotenumList.size()==0){
					//今天还没有获得积分
					if((int)integral>Integer.parseInt(pointsLimit)){
						//观看视频得到的积分大于能够获取的积分
						integralCount = Integer.parseInt(pointsLimit);
					}else{
						//观看视频得到的积分小于能够获取的积分
						integralCount = (int)integral;
					}
				}else{
					int votenumTotal = 0;
					for (AcStbUserVotenum acStbUserVotenum : acStbUserVotenumList) {
						votenumTotal = votenumTotal + acStbUserVotenum.getF_asuv_votenum();
					}
					if(Integer.parseInt(pointsLimit)>votenumTotal){
						//能够获得的积分上限大于今天已经获得的积分，还可以获得积分
						int remainingPoints = (Integer.parseInt(pointsLimit)) - votenumTotal;
						if((int)integral>remainingPoints){
							//此次观看视频获得的积分大于当天能够获得的积分，只能得到能够获得的积分
							integralCount = remainingPoints;
						}else{
							//此次观看视频获得的积分小于当天能够获得的积分，能够获得观看积分
							integralCount = (int)integral;
						}
					}else{
						//今天的积分已经获得上限，不能够继续获得积分
						map.put("playTime",pt);
						LOG.debug("播放开始时间:"+playTime+"/观看总时间:"+pt+"/updateTime:"+time1+"/serverid:"+awv.getF_awl_serverid()+"/activityid:"+awv.getF_awl_activityid());
						map.put("status",status);
						return map;
					}
				}
				
				AcStbUserVotenum acvote1 = new AcStbUserVotenum();
				acvote1.setF_asuv_serverid(awv.getF_awl_serverid());
				acvote1.setF_asuv_votenum(integralCount);
				acvote1.setF_asuv_activityid(awv.getF_awl_activityid());
				acvote1.setF_asuv_source("6");
				acvote1.setF_asuv_memo("追踪王将观看视频获得积分");
				acvote1.setF_asuv_resolution(awv.getF_ui_resolution());
				insertFeedNum(acvote1);
				
				//修改用户等级
				CmSvcVoteTotalVo csvt = new CmSvcVoteTotalVo();
				csvt.setF_csvt_activityid(awv.getF_awl_activityid());
				csvt.setF_csvt_obligate1(awv.getF_awl_serverid());
				List<CmSvcVoteTotalVo> myList = acIntegralDao.selectCmSvcVoteTotal(csvt);
				if(myList.size()>0) {
					csvt = myList.get(0);
					if(csvt.getF_csvt_remark()==null || "".equals(csvt.getF_csvt_remark())) {
						if(csvt.getF_csvt_votecount()>=Integer.parseInt(kingThreshold)) {
							csvt.setF_csvt_remark("3");
							acIntegralDao.updateCmSvcVoteTotalVoRemark(csvt);
						}else if(csvt.getF_csvt_votecount()>=Integer.parseInt(masterThreshold)) {
							csvt.setF_csvt_remark("2");
							acIntegralDao.updateCmSvcVoteTotalVoRemark(csvt);
						}else {
							csvt.setF_csvt_remark("1");
							acIntegralDao.updateCmSvcVoteTotalVoRemark(csvt);
						}
					}else if(Integer.parseInt(csvt.getF_csvt_remark())>=3) {
						
					}else if(Integer.parseInt(csvt.getF_csvt_remark())==2) {
						if(csvt.getF_csvt_votecount()>=Integer.parseInt(kingThreshold)) {
							csvt.setF_csvt_remark("3");
							acIntegralDao.updateCmSvcVoteTotalVoRemark(csvt);
						}
					}else {
						if(csvt.getF_csvt_votecount()>=Integer.parseInt(kingThreshold)) {
							csvt.setF_csvt_remark("3");
							acIntegralDao.updateCmSvcVoteTotalVoRemark(csvt);
						}else if(csvt.getF_csvt_votecount()>=Integer.parseInt(masterThreshold)) {
							csvt.setF_csvt_remark("2");
							acIntegralDao.updateCmSvcVoteTotalVoRemark(csvt);
						}
					}
				}
			}
			map.put("playTime",pt);
			LOG.debug("播放开始时间:"+playTime+"/观看总时间:"+pt+"/updateTime:"+time1+"/serverid:"+awv.getF_awl_serverid()+"/activityid:"+awv.getF_awl_activityid());
		}else if(status!=null && "2".equals(status)){
			CmSvcVoteTotalVo csvt = new CmSvcVoteTotalVo();
			csvt.setF_csvt_activityid(awv.getF_awl_activityid());
			csvt.setF_csvt_obligate1(awv.getF_awl_serverid());
			csvt.setF_csvt_votecount(Integer.parseInt(convertIntegral));
			acIntegralDao.updateCmSvcVoteTotalVo(csvt);
		}
		map.put("status",status);
		return map;
	}
}
